package com.newstart.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.newstart.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * Created by liushijie on 2018/5/28.
 */
@Service
public class StartService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallback")
    public User getUser(Long id) {
        return restTemplate.getForObject("http://server/user/" + id, User.class);
    }

    public User fallback(Long id) {
        User u = new User();
        u.setNickname("fallback");
        return u;
    }

    @HystrixCommand(fallbackMethod = "fallback1")
    public String getSession() {
        return restTemplate.getForObject("http://server/session/", String.class);
    }

    @HystrixCommand(fallbackMethod = "fallback2")
    public void setSession() {
        restTemplate.put("http://server/session/", String.class);
    }

    public String fallback1() {
        return "fallback";
    }

    public void fallback2() {
    }
}
